package com.example.echoewish.ui.my_activity.my_friend_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.example.echoewish.R;
import com.example.echoewish.pojo.MyConcern;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.ui.EchoWishCompatActivity;
import com.example.echoewish.ui.my_activity.chat_activity.ChatActivity;
import com.example.echoewish.ui.my_activity.user_info_activity.UserInfoShowActivity;

import java.util.LinkedList;
import java.util.List;

public class MyConcernActivity extends EchoWishCompatActivity {

    private int CHANGE_LIST_VIEW=4;
    private ListView myFriendListView;
    private MyConcernAdapter myConcernAdapter;
    private List<MyConcern> myConcernList;

    private MyConcernViewModel myConcernViewModel;

    private Handler MyFriendActivityHandler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    if(msg.arg1==CHANGE_LIST_VIEW)
                    {
                        myConcernAdapter.clear();
                        myConcernAdapter.addAll(myConcernViewModel.friendMutableLiveData.getValue());
                        myConcernAdapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myConcernViewModel.getMyFriendMessage();
        myConcernAdapter.setMsgButtonListener(new MyConcernAdapter.MsgButtonListener() {
            @Override
            public void onClickListener(int position) {
                ChatActivity.activityStart(MyConcernActivity.this,myConcernViewModel.friendMutableLiveData.getValue().get(position));
            }

            @Override
            public void headImageClickListener(int position) {
                UserInfoShowActivity.activityStart(MyConcernActivity.this,myConcernViewModel.friendMutableLiveData.getValue().get(position).friId);
                finish();
            }

        });
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_my_friend);
    }

    public void initView()
    {

        myFriendListView=findViewById(R.id.my_friend_list_view);
        myConcernList=new LinkedList<>();
        myConcernAdapter =new MyConcernAdapter(this,R.layout.my_friend_item,myConcernList);
        myFriendListView.setAdapter(myConcernAdapter);
    }

    @Override
    public void initViewModel() {
        myConcernViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyConcernViewModel.class);
    }

    @Override
    public void observeLiveData() {
        myConcernViewModel.friendMutableLiveData.observe(this, new Observer<List<MyConcern>>() {
            @Override
            public void onChanged(List<MyConcern> myConcerns) {
                Message message=new Message();
                message.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                message.arg1= CHANGE_LIST_VIEW;
                MyFriendActivityHandler.sendMessage(message);
            }
        });
    }

}